package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: zm
 * @Date: 2021/8/10 19:11
 */
@Service
public class ItemServiceImpl implements ItemService {
    // 远程调用service-product-client
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;




    //  定义数据接口： Thymeleaf 渲染数据时，可以在后台使用存储Map ,返回一个map
    //  页面渲染的时候：将页面渲染的key 放入map 中！ 数据汇总 数据是从service-product 中来！

    @Override
    public Map<String, Object> getItemBySkuId(Long skuId) {
        // 声明返回结果对象
        Map<String, Object> result = new HashMap<>();
        // 获取skuInfo对象
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        // 获取skuInfo 价格
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal price = productFeignClient.getSkuPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        // 获取分类数据
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", categoryView);
        }, threadPoolExecutor);

        // 获取到销售属性+销售属性值+锁定
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        // 获取到销售属性值Id 与 skuId 组成的map 集合
        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map idsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String idsJson = JSONObject.toJSONString(idsMap);
            result.put("valuesSkuJson", idsJson);
        }, threadPoolExecutor);

        CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);


        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                skuPriceCompletableFuture,
                categoryViewCompletableFuture,
                spuSaleAttrListCompletableFuture,
                valuesSkuJsonCompletableFuture,
                incrHotScoreCompletableFuture
        ).join();

        return result;
    }

//    @Override
//    public Map<String, Object> getItemBySkuId(Long skuId) {
//        // 声明返回结果对象
//        Map<String,Object> result = new HashMap<>();
//
//        // 获取skuInfo对象
//        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
//        // 获取skuInfo 价格
//        BigDecimal price = productFeignClient.getSkuPrice(skuId);
//
//        result.put("skuInfo",skuInfo);
//        result.put("price",price);
//
//        if(skuInfo != null){
//            // skuInfo对象不等于null, 获取分类数据
//            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
//            result.put("categoryView",categoryView);
//
//            //  获取到销售属性+销售属性值+锁定
//            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
//            result.put("spuSaleAttrList",spuSaleAttrListCheckBySku);
//
//              // 获取到销售属性值Id 与 skuId 组成的map 集合
//            Map idsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
//            String idsJson = JSONObject.toJSONString(idsMap);
//            result.put("valuesSkuJson",idsJson);
//
//        }
//        return result;
//    }
}
